/**
 * Find power-set of a set using BITWISE approach.
 *
 * @param {*[]} val
 * @return {*[][]}
 */
export default function bwPowerSet(val: any[]) {
  const subSets = []

  const num = 2 ** val.length

  for (let i = 0; i < num; i++) {
    const subSet = []

    for (let index = 0; index < val.length; index++) {
      // Decide whether we need to include current element into the subset or not.
      if (i & (1 << index)) {
        subSet.push(val[index])
      }
    }

    subSets.push(subSet)
  }

  return subSets
}
